package com.brkj.communityStudy;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.brkj.communityStudy.model.DS_TopicStudy;
import com.brkj.httpInterface.BaseAjaxParams;
import com.brkj.httpInterface.HttpInterface;
import com.brkj.httpInterface.NomalResult;
import com.brkj.main.R;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.util.MyApplication;
import com.brkj.util.view.BaseCoreActivity;
import net.tsz.afinal.FinalHttp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by colin.ding on 2014/5/16.
 */

public class TopicStudyAdapter extends BaseAdapter {
    private ListItemView listItemView;
    protected LayoutInflater listContainer;
    private Context context;
    private List<DS_TopicStudy> topicList;

    private Map<Integer, Integer> haveAddMap = new HashMap<Integer, Integer>();
    private boolean isMine = false;

    public TopicStudyAdapter(Context context, List<DS_TopicStudy> topicList, boolean isMine){
        this.context = context;
        this.topicList = topicList;
        this.isMine = isMine;

        listContainer = LayoutInflater.from(context);

    }

    private static class ListItemView{
        public TextView Title;
        public TextView Time;
        public TextView joinNum;
        public Button join;
        public ImageView hotTopic;
    };

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return topicList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // 自定义视图
        listItemView = new ListItemView();
        // 获取list_item布局文件的视图
        if (convertView == null) {
            convertView = listContainer.inflate(R.layout.topic_study_item, null);
            // 获取控件对象
            listItemView.Title = (TextView) convertView.findViewById(R.id.Title);
            listItemView.Time = (TextView) convertView
                    .findViewById(R.id.Time);
            listItemView.joinNum = (TextView) convertView
                    .findViewById(R.id.joinNum);
            listItemView.Time = (TextView) convertView
                    .findViewById(R.id.Time);
            listItemView.join = (Button) convertView
                    .findViewById(R.id.join);
            listItemView.hotTopic = (ImageView) convertView
                    .findViewById(R.id.hotTopic);

            // 设置控件集到convertView
            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }

        listItemView.Title.setText(topicList.get(position).getTitle());
        listItemView.Time.setText(topicList.get(position).getTime());
        listItemView.joinNum.setText("参与人数：" + topicList.get(position).getJoinNum());

        if((topicList.get(position).getType() == 1) || (haveAddMap.containsKey(position))){
            listItemView.hotTopic.setVisibility(View.VISIBLE);
        }else{
            listItemView.hotTopic.setVisibility(View.GONE);
        }

        if(isMine){
            listItemView.join.setVisibility(View.GONE);
        }else if(topicList.get(position).getIsJoin() == 0){
            listItemView.join.setVisibility(View.VISIBLE);
            listItemView.join.setTag(position);

            listItemView.join.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    int pos = (Integer) v.getTag();
                    BaseAjaxParams params = new BaseAjaxParams();
                    params.put(HttpInterface.HIF_JoinTopicStudy.params.topicID,
                            Integer.toString(topicList.get(pos).getTpiocID()));
                    params.put(HttpInterface.HIF_JoinTopicStudy.params.UserID, MyApplication.myUserID);

                    FinalHttp fh = new FinalHttp();
                    fh.post(HttpInterface.HIF_JoinTopicStudy.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)context) {
                        @Override
                        public void onSuccess(Object o) {
                            super.onSuccess(o);

                            if(NomalResult.doResult(o, context)){
                                v.setVisibility(View.GONE);
                                v.setOnClickListener(null);

                                haveAddMap.put(position, position);
                            }
                        }
                    });

                }
            });
        }else{
            listItemView.join.setVisibility(View.GONE);
        }


        return convertView;
    }

//   class MyOnClickListener implements View.OnClickListener{
//
//   }
}
